package teamwork.pc.action;

import javax.annotation.Resource;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

import teamwork.pc.dao.StaffDao;
import teamwork.pc.entity.Staff;
import teamwork.pc.form.IndexForm;
import teamwork.pc.service.LoginService;

public class IndexAction extends AbstractAction{
    @ActionForm
    @Resource
    public IndexForm indexForm;
    @Resource
    protected LoginService loginService;
    @Resource
    protected StaffDao staffDao;

    /**
     *
     */
    //abc xyz
    @Execute(validator = false, removeActionForm = true)
    public String index() {
        return "index.jsp";
    }
    /**
     *
     */
   @Execute(validator = true, validate = "doLoginValidate", input = "index", removeActionForm = true)
    public String doLogin()
    {
        Staff s = staffDao.findByEmailAndPassword(indexForm.account, indexForm.password);
        loginUserDto.login(s);
        return "home?redirect=true";
    }

    public ActionMessages doLoginValidate(){
        ActionMessages errors = new ActionMessages();
        if(!loginService.canLogin(indexForm.account, indexForm.password)){
            errors.add("invalidAccountOrPassword",new ActionMessage("login.error.invalidAccountOrPassword"));
        }
        return errors;
    }
}